﻿using FAMailUtils;
using FAMailUtils.Data.DAO;
using FAMailUtils.Data.DTO;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OnlineBusinesses.Areas.CustomerAdmin.MailBox
{
    public partial class ViewGroupsMails : System.Web.UI.Page
    {
        GroupDAO dao = new GroupDAO();
        DepartmentDAO dDao = new DepartmentDAO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    int id = 0;
                    int.TryParse(Request.QueryString["id"], out id);
                    if (id > 0)
                    {
                        GroupDTO group = dao.GetByGroupID(id, ((AdminCustomer_Master)Page.Master).getUserLogin().Client.UserID);
                        if (group.GroupID > 0)
                        {
                            hdGroupID.Value = group.GroupID.ToString();
                            lbGroupName.Text = group.GroupName;
                            lbGroupDesc.Text = group.Description;
                            lvGroup.DataSource = dao.GetAccountByGroupID(group.GroupID, group.UserID);
                            lvGroup.DataBind();
                        }
                    }
                    ddlDepartment.DataValueField = "DepartmentID";
                    ddlDepartment.DataTextField = "DepartmentName";
                    DataTable Source = dDao.GetAll(((AdminCustomer_Master)Page.Master).getUserLogin().Client.UserID);
                    DataRow row = Source.NewRow();
                    row["DepartmentID"] = 0;
                    row["DepartmentName"] = "Tất cả";
                    Source.Rows.InsertAt(row, 0);
                    ddlDepartment.DataSource = Source;
                    ddlDepartment.DataBind();
                }
                catch (Exception ex)
                {
                    Common.logs(Request.RawUrl + "-" + Common.GetCurrentMethod(), ex, Common.logType.error);
                }
            }
        }

        protected void btnDel_Click(object sender, CommandEventArgs e)
        {
            try
            {
                int groupID = int.Parse(hdGroupID.Value);
                int accountID = Convert.ToInt32(e.CommandArgument);
                dao.RemoveAccountFromGroup(groupID, accountID);
                Response.Redirect(Request.RawUrl);
            }
            catch (Exception ex)
            {
                Common.logs(Request.RawUrl + "-" + Common.GetCurrentMethod(), ex, Common.logType.error);
            }
        }

        protected void ddlDepartment_SelectedIndexChanged(object sender, EventArgs e)
        {
            int groupid = 0;
            int.TryParse(Request.QueryString["id"], out groupid);
            if (groupid > 0)
            {
                int dID = Convert.ToInt32(ddlDepartment.SelectedValue);
                ddlAccount.Items.Clear();
                ddlAccount.DataValueField = "AccountID";
                ddlAccount.DataTextField = "mix";
                DataTable Source = dao.GetAccountByDepartmentID(dID, groupid, ((AdminCustomer_Master)Page.Master).getUserLogin().Client.UserID);
                ddlAccount.DataSource = Source;
                ddlAccount.DataBind();
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            int groupid = 0;
            int.TryParse(Request.QueryString["id"], out groupid);
            if (groupid > 0)
            {
                int accountid = 0;
                if (int.TryParse(ddlAccount.SelectedValue, out accountid))
                {
                    dao.AddAccountToGroup(accountid, groupid);
                    Response.Redirect(Request.RawUrl, true);
                }
                else { }
            }
        }

    }
}